package org.sunyaxing.transflow.transflowapp.common;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChainManager {
    // 单例静态缓存
    public final static Map<String, TransFlowChain<?>> CHAIN_CACHE = new ConcurrentHashMap<>();

    public static void addChainCache(TransFlowChain<?> chain) {
        // 如果节点已经在全局中存在，就销毁重建
        removeChainCache(chain.getNodeId());
        // 重新放到缓存
        CHAIN_CACHE.put(chain.getNodeId(), chain);
    }

    public static void removeChainCache(String nodeId) {
        // 销毁线程
        TransFlowChain<?> transFlowChain = CHAIN_CACHE.get(nodeId);
        if (transFlowChain != null) {
            transFlowChain.dispose();
            // 移除引用
            CHAIN_CACHE.remove(nodeId);
        }
    }

    public static TransFlowChain<?> getChainCache(String nodeId) {
        return CHAIN_CACHE.get(nodeId);
    }

    public static boolean containsChain(String nodeId) {
        return CHAIN_CACHE.containsKey(nodeId);
    }
}
